﻿
Imports Sunts.HyperV.Common

Public Class frmDetectedServers
    Public Property HyperVHostMachine As HyperVHost
 

    Private Sub DetectedServers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            dgvDetectedServers.Rows.Clear()

            If HyperVHostMachine Is Nothing Then
                Exit Sub
            End If

            dgvDetectedServers.DataSource = CheckForVirtualMachines(HyperVHostMachine)

            dgvDetectedServers.Columns("Selected").Width = 60
            dgvDetectedServers.Columns("HostID").Visible = False
            dgvDetectedServers.Columns("VirtualMachineID").Visible = False

        Catch ex As Exception
            MessageBox.Show("frmDetectedServers: DetectedServers_Load: " & ex.Message)
        End Try
    End Sub
 
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        HyperVHostMachine = Nothing
        Me.Close()
    End Sub

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim VirtualMachine As VirtualMachine

        Try
            For Each dr As DataGridViewRow In dgvDetectedServers.Rows
                If CBool(dr.Cells("Selected").Value) = True Then
                    VirtualMachine = New VirtualMachine
                    VirtualMachine.VirtualMachineGuid = CStr(dr.Cells("VirtualMachineGuid").Value)
                    VirtualMachine.VirtualMachineName = CStr(dr.Cells("VirtualMachineName").Value)
                    VirtualMachine.HostID = HyperVHostMachine.HostID
                    VirtualMachine.Insert(My.Settings.ConnectionString)
                End If
            Next
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("frmDetectedServers: btnSubmit_Click: " & ex.Message)
        End Try

    End Sub


End Class